package com.example.connection;


import org.junit.Test;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionTest {

    // 获取数据库连接的方式一：直接使用Driver接口的connect方法获取连接，Driver接口使用具体数据库驱动中的Driver实现类初始化
    @Test
    public void testConnection1() throws SQLException {
        Driver driver = new com.mysql.cj.jdbc.Driver();

        String url = "jdbc:mysql://60.60.60.223:30335/test";

        Properties properties = new Properties();
        properties.put("user", "root");
        properties.put("password", "admin1#3");


        Connection connection = driver.connect(url, properties);
        System.out.println(connection);
    }

    // 获取数据库连接的方式二：直接使用Driver接口的connect方法获取连接，Driver接口通过反射获取具体数据库驱动中的Driver实现类
    @Test
    public void testConnection2() throws Exception {
        Class<?> clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        String url = "jdbc:mysql://60.60.60.223:30335/test";

        Properties properties = new Properties();
        properties.put("user", "root");
        properties.put("password", "admin1#3");


        Connection connection = driver.connect(url, properties);
        System.out.println(connection);
    }

    // 获取数据库连接的方式三：使用DriverManager代替Driver接口，使用DriverManager的getConnection方法获取连接
    @Test
    public void testConnection3() throws Exception {
        Class<?> clazz = Class.forName("com.mysql.cj.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        String url = "jdbc:mysql://60.60.60.223:30335/test";
        String user = "root";
        String password = "admin1#3";

        DriverManager.registerDriver(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    // 获取数据库连接的方式四：使用DriverManager代替Driver接口，使用DriverManager的getConnection方法获取连接，
    // 省略DriverManager的registerDriver的操作
    @Test
    public void testConnection4() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");

        String url = "jdbc:mysql://60.60.60.223:30335/test";
        String user = "root";
        String password = "admin1#3";

        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

    // 获取数据库连接的方式五：将数据库连接配置声明在配置文件中，通过读取配置文件的方式获取连接
    @Test
    public void testConnection5() throws Exception {
        InputStream inputStream = ConnectionTest.class.getClassLoader()
                .getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(inputStream);

        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driverClass = properties.getProperty("driverClass");

        Class.forName(driverClass);
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }

}
